﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DotNetDream.WebMetroEngine.Mvc.Models;

namespace DotNetDream.WebMetroEngine.Mvc.Context
{
    public class ModuleContext : DbContext
    {
        public DbSet<Domain> Domains { get; set; }
        public DbSet<Module> Modules { get; set; }
        public DbSet<Tile> Tiles { get; set; }
        public DbSet<ModuleTile> ModuleTiles { get; set; }
        public DbSet<TileType> TileTypes { get; set; }
        public DbSet<TileLayout> TileLayouts { get; set; }

        public ModuleContext() : base("DotNetDream.WebMetroEngine.Mvc.Module") { }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            #region Tables mapping
            //Domain
            modelBuilder.Entity<Domain>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Domain");
            });
            //Module
            modelBuilder.Entity<Module>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Module");
            });
            //Tile
            modelBuilder.Entity<Tile>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Tile");
            });
            //ModuleTiles
            modelBuilder.Entity<Module>()
                .HasMany(m => m.Tiles)
                .WithMany(t => t.Modules)
                .Map(mt =>
            {
                mt.ToTable("ModuleTiles");
                mt.MapLeftKey("ModuleId");
                mt.MapRightKey("TileId");
            });
            //TileType
            modelBuilder.Entity<TileType>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("TileType");
            });
            //TileLayout
            modelBuilder.Entity<TileLayout>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("TileLayout");
            });
            #endregion

            #region Keys mapping
            modelBuilder.Entity<ModuleTile>().HasKey(m => new { m.ModuleId, m.TileId });
            #endregion

            #region Children lazy loading
            //Domain has many Modules
            modelBuilder.Entity<Domain>()
                .HasMany(d => d.Modules)
                .WithRequired(m => m.Domain)
                .HasForeignKey(m => m.DomainId);
            //Tile has many Tile children
            modelBuilder.Entity<Tile>()
                .HasMany(t => t.Childrens)
                .WithRequired(p => p.Parent)
                .HasForeignKey(p => p.ParentTileId);
            //Type has many Tiles
            modelBuilder.Entity<TileType>()
                .HasMany(tt => tt.Tiles)
                .WithRequired(t => t.Type)
                .HasForeignKey(t => t.TypeId);
            //Layout has many Tiles
            modelBuilder.Entity<TileLayout>()
                .HasMany(tt => tt.Tiles)
                .WithRequired(t => t.Layout)
                .HasForeignKey(t => t.LayoutId);
            #endregion
        }
    }
}
